﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.PowerPoint;

namespace BAASearchLib
{
    public class PptToText
    {
        private string filePath;
        private object objFalse = false;
        private object objTrue = true;
        private object missing = System.Reflection.Missing.Value;
        private object emptyData = string.Empty;
        private string author = "";

        public PptToText(string filePath)
        {
            this.filePath = filePath;
        }

        /// <summary>
        /// Opens actually the file in the background 
        /// -> so it is not so fast, may be fixed in the second release (which we will not have) *sad and cry painfully*
        /// </summary>
        /// <returns></returns>
        public string ExtractText()
        {
            Microsoft.Office.Interop.PowerPoint.Presentations objPresSet;
            Microsoft.Office.Interop.PowerPoint._Presentation objPres = null;
            Microsoft.Office.Interop.PowerPoint.Slides objSlides;
            try
            {
                //Create a new presentation based on a template.
                objPresSet = PptToTextPresentationApp.PpApp.Presentations;
                objPres = objPresSet.Open(filePath,
                    MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoFalse);
                objSlides = objPres.Slides;

                StringBuilder sb = new StringBuilder();
                // loop through all the slides
                for (int pos = 1; pos < objSlides.Count; pos++)
                {
                    Microsoft.Office.Interop.PowerPoint.Shapes sld = objPres.Slides[pos].Shapes;
                    // loop trough all the Shapes
                    foreach (Microsoft.Office.Interop.PowerPoint.Shape s in sld)
                    {
                        // Check to see if shape has a text frame and text
                        if (s.HasTextFrame == MsoTriState.msoTrue
                            && s.TextFrame.HasText == MsoTriState.msoTrue)
                        {
                            if (s.Type.Equals(MsoShapeType.msoPlaceholder))
                            {
                                switch (s.PlaceholderFormat.Type)
                                {
                                    case PpPlaceholderType.ppPlaceholderTitle:
                                        sb.Append(s.TextFrame.TextRange.Text);
                                        sb.Append(Environment.NewLine);
                                        break;
                                    case PpPlaceholderType.ppPlaceholderCenterTitle:
                                        sb.Append(s.TextFrame.TextRange.Text);
                                        sb.Append(Environment.NewLine);
                                        break;
                                    case PpPlaceholderType.ppPlaceholderBody:
                                        sb.Append(s.TextFrame.TextRange.Text);
                                        sb.Append(Environment.NewLine);
                                        break;
                                    case PpPlaceholderType.ppPlaceholderSubtitle:
                                        sb.Append(s.TextFrame.TextRange.Text);
                                        sb.Append(Environment.NewLine);
                                        break;
                                    case PpPlaceholderType.ppPlaceholderFooter:
                                        sb.Append(s.TextFrame.TextRange.Text);
                                        sb.Append(Environment.NewLine);
                                        break;
                                    case PpPlaceholderType.ppPlaceholderHeader:
                                        sb.Append(s.TextFrame.TextRange.Text);
                                        sb.Append(Environment.NewLine);
                                        break;
                                    default:
                                        // don't need anything else
                                        break;
                                }
                            }
                        }
                    }
                }

                /*
                // -----------------Get Note Text-----------------
                // loop through all the slides
                for (int pos = 1; pos < objSlides.Count; pos++)
                {
                    string notesText = "";
                    notesText = notesText + "Slide" + objPres.Slides[pos].SlideIndex + "\n";
                    Microsoft.Office.Interop.PowerPoint.Shapes sld = objPres.Slides[pos].NotesPage.Shapes;
                    // loop trough all the Shapes
                    foreach (Microsoft.Office.Interop.PowerPoint.Shape s in sld)
                    {
                        if (s.HasTextFrame == MsoTriState.msoTrue)
                            if (s.TextFrame.HasText == MsoTriState.msoTrue)
                            {
                                notesText = notesText + s.TextFrame.TextRange.Text;
                                Debug.WriteLine("+: " + s.Table.FirstCol.ToString());
                            }
                    }
                    notesText = notesText + "\n";
                    Debug.WriteLine("NOTES-TEXT: " + notesText);
                }
                //-----------------------------------------------
                */

                return sb.ToString();
            }
            catch (Exception e)
            {
                throw new ArgumentException(e.Message);
            }
            finally
            {
                (objPres as _Presentation).Close();//Close(ref objFalse, ref missing, ref missing);
            }
        }

        public string GetAuthor()
        {
            Microsoft.Office.Interop.PowerPoint.Presentations objPresSet;
            Microsoft.Office.Interop.PowerPoint._Presentation objPres = null;
            try
            {
                objPresSet = PptToTextPresentationApp.PpApp.Presentations;
                objPres = objPresSet.Open(filePath,
                    MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoFalse);
                // -----------Gets the Author-----------
                object oDocBuiltInProps = objPres.BuiltInDocumentProperties;
                Type typeDocBuiltInProps = oDocBuiltInProps.GetType();

                // Get the Author property and display it.
                string strIndex = "Author";
                string strValue;
                object oDocAuthorProp = typeDocBuiltInProps.InvokeMember("Item",
                                           BindingFlags.Default |
                                           BindingFlags.GetProperty,
                                           null, oDocBuiltInProps,
                                           new object[] { strIndex });
                Type typeDocAuthorProp = oDocAuthorProp.GetType();
                strValue = typeDocAuthorProp.InvokeMember("Value",
                                           BindingFlags.Default |
                                           BindingFlags.GetProperty,
                                           null, oDocAuthorProp,
                                           new object[] { }).ToString();
                this.author = strValue;
                //MessageBox.Show("The Author is: " + strValue, "Author");
                // --------------------------------------------------
            }
            catch (Exception e)
            {
                throw new ArgumentException(e.Message);
            }
            finally
            {
                (objPres as _Presentation).Close();
            }


            return author;
        }
    }
}
